<?php
/**
 * 常规同城活动-线上广宣
 * @author jianhui
 *
 */
class cityGxModel extends MY_Model{
    function cityGxModel(){
        parent::MY_Model();
        $this->table = 'CityGx';
    }
    /**
     * 更新父类金额(non-PHPdoc)
     * @see MY_Model::updateParentMoneys()
     */
    function updateParentMoneys($parentId){
        if ($parentId['projectId']){
            $this->CI->load->model('cityProjectModel');
            $this->CI->cityProjectModel->updateMoney($parentId['projectId']);
        }elseif ($parentId['procId']){
            $this->CI->load->model('cityProcessModel');
            $this->CI->cityProcessModel->updateMoney($parentId['procId']);
        }
    }
    /**
     * 获取父级关联ID(non-PHPdoc)
     * @see MY_Model::getParentId()
     */
    function getParentId($data){
        if (isset($data['projectId']) && (int)$data['projectId']){
            return array('projectId'=>$data['projectId']);
        }elseif (isset($data['procId']) && (int)$data['procId']){
            return array('procId'=>(int)$data['procId']);            
        }else {
            return false;
        }
    }
    /**
     * 包装数据(non-PHPdoc)
     * @see MY_Model::packing()
     */
    function packing($list){
        $fileIds = array();
        foreach ($list as $v){
            if($v['fileId'] && !in_array($v['fileId'], $fileIds)){
                $fileIds[] = (int)$v['fileId'];
            }
        }
        //附件数据
        $this->CI->load->model('fileModel');
        $fileList = $this->CI->fileModel->getList(array('id'=>array('in',$fileIds)));
        //一级媒体
        $gxMediaType1 = $this->config->item('gxMediaType1');
        //二级媒体
        $gxMediaType2 = $this->config->item('gxMediaType2');
        //常规媒体规格
        $gxMediaType21Size = $this->config->item('gxMediaType21Size');
        //角色列表
        $roleList = $this->config->item('roleList');
        //审核结果
        $auditRs = $this->config->item('auditRs');
        foreach ($list as $k => $v){
            //一级媒体名称
            $list[$k]['mediaType1Str'] = isset($gxMediaType1[$v['mediaType1']]) ? $gxMediaType1[$v['mediaType1']] : '';
            /* if ($v['mediaType1'] == -1){
                $list[$k]['mediaType1Str'] .= '-'.$v['mediaType1Alias'];
            } */
            //二级媒体名称
            if ($v['mediaType1'] != -1){
                $list[$k]['mediaType2Str'] = isset($gxMediaType2[$v['mediaType1']][$v['mediaType2']]) ? $gxMediaType2[$v['mediaType1']][$v['mediaType2']] : '';
                $v['mediaType2Alias'] && $list[$k]['mediaType2Str'] .= '-'.$v['mediaType2Alias'];
            }else{
                $list[$k]['mediaType2Str'] = $v['mediaType1Alias'];
            }
            //媒体规格
            if ($v['mediaType1'] == 1){//常规媒体
                $list[$k]['mediaSizeStr'] = isset($gxMediaType21Size[$v['mediaType2']][$v['mediaSize']]) ? $gxMediaType21Size[$v['mediaType2']][$v['mediaSize']] : '';
                $v['mediaSize'] == -1 && $list[$k]['mediaSizeStr'] .= '-'. $v['mediaSizeAlias1'];
                if ($v['mediaType2'] == 1){
                    $list[$k]['mediaSizeStr'] .= '<br>报纸版面：'.$v['mediaSizeAlias2'];
                }
            }elseif (in_array($v['mediaType1'], array(3))){//户外大牌
                $list[$k]['mediaSizeStr'] = '长：'.$v['mediaSizeAlias1'].'cm';
                $list[$k]['mediaSizeStr'] .= '<br>高：'.$v['mediaSizeAlias2'].'cm';
            }else{//其它
                $list[$k]['mediaSizeStr'] = $v['mediaSizeAlias1'];
            }
            //预算审核状态
            $list[$k]['budgetAuditStr'] = isset($roleList[$v['budgetAuditRole']]) ? $roleList[$v['budgetAuditRole']].'已审核' : '';
            if ($list[$k]['budgetAuditStr']){
                $list[$k]['budgetAuditStr'] .= isset($auditRs[$v['budgetAuditRs']]) ? ','.$auditRs[$v['budgetAuditRs']] : '';
            }else{
                $list[$k]['budgetAuditStr'] = isset($auditRs[$v['budgetAuditRs']]) ? $auditRs[$v['budgetAuditRs']] : '';
            }
            //结算审核状态
            $list[$k]['settleAuditStr'] = isset($roleList[$v['settleAuditRole']]) ? $roleList[$v['settleAuditRole']].'已审核' : '';
            if ($list[$k]['settleAuditStr']){
                $list[$k]['settleAuditStr'] .= isset($auditRs[$v['settleAuditRs']]) ? ','.$auditRs[$v['settleAuditRs']] : '';
            }else{
                $list[$k]['settleAuditStr'] = isset($auditRs[$v['settleAuditRs']]) ? $auditRs[$v['settleAuditRs']] : '';
            }
            //附件数据
            $list[$k]['fileData'] = isset($fileList[$v['fileId']]) ? $fileList[$v['fileId']] : array();
        }
        return $list;
    }
    /**
     * 统计对应父级ID的金额
     * @param int $procId
     * @return array
     */
    function getMoney($where = array()){
        $data = array(
            'budgetApply' => 0,
            'budgetAudit' => 0,
            'settleApply' => 0,
            'settleAudit' => 0,
            'buBudgetApply' => 0,
            'buBudgetAudit' => 0,
            'buSettleApply' => 0,
            'buSettleAudit' => 0,
        );
        if ($where['projectId']){
            $findList = $this->getList(array('projectId'=>$where['projectId']));
        }else if ($where['procId']){
            $findList = $this->getList(array('procId'=>$where['procId']));            
        }else{
            return $data;
        }
        foreach ($findList as $v){
            if ($v['isBubao'] == 0){
                $data['budgetApply'] += $v['budgetApply'];
                in_array($v['budgetAuditRs'], array(1,4)) && $data['budgetAudit'] += $v['budgetAudit'];
                if ($v['isSettle'] == 1){
                    $data['settleApply'] += $v['settleApply'];
                    in_array($v['settleAuditRs'], array(1,4)) && $data['settleAudit'] += $v['settleAudit'];
                }
            }else{
                $data['buBudgetApply'] += $v['budgetApply'];
                in_array($v['budgetAuditRs'], array(1,4)) && $data['buBudgetAudit'] += $v['budgetAudit'];
                if ($v['isSettle'] == 1){
                    $data['buSettleApply'] += $v['settleApply'];
                    in_array($v['settleAuditRs'], array(1,4)) && $data['buSettleAudit'] += $v['settleAudit'];
                }
            }
        }
        return $data;
    }
    /**
     * 预算审核状态统计，主线流程ID
     * @param int $procId
     * @return multitype:
     */
    function getBudgetStatusCountProcId($procId, $isBubao=0) {
        $findList = $this->getList(array('procId'=>$procId, 'isBubao'=>$isBubao,'budgetApply > '=>0), null, null, 'budgetAuditRs');
        $count = array();
        foreach ($findList as $v){
            $count[$v['budgetAuditRs']] ++;
        }
        return $count;
    }
    /**
     * 预算审核状态统计，项目ID
     * @param unknown $projectId
     * @param number $isBubao
     */
    function getBudgetStatusCountProjectId($projectId) {
        $findList = $this->getList(array('projectId'=>$projectId, 'budgetApply > '=>0), null, null, 'budgetAuditRs');
        $count = array();
        foreach ($findList as $v){
            $count[$v['budgetAuditRs']] ++;
        }
        return $count;
    }
    /**
     * 广宣通过主线ID一键预算审核
     * @param int $procId
     * @param number $audit
     * @param array $user
     */
    function budgetAuditByProcId($procId, $audit = 1,$user = array()){
        $findList = $this->getList(array('procId'=>$procId));
        foreach ($findList as $v){
            $auditData = array(
                'budgetAudit' => $audit==1 ? $v['budgetApply'] : 0,
                'budgetAuditRs' => $audit,
                'budgetAuditRole' => $user['userRole']
            );
            $this->db->update($this->table, $auditData, array('id'=>$v['id']));
        }
    }
    /**
     * 广宣通过项目ID一键预算审核
     * @param int $procId
     * @param number $audit
     * @param array $user
     */
    function budgetAuditByProjectId($projectId, $audit = 1,$user = array()){
        $findList = $this->getList(array('projectId'=>$projectId));
        foreach ($findList as $v){
            $auditData = array(
                'budgetAudit' => $audit==1 ? $v['budgetApply'] : 0,
                'budgetAuditRs' => $audit,
                'budgetAuditRole' => $user['userRole']
            );
            $this->db->update($this->table, $auditData, array('id'=>$v['id']));
        }
    }
    /**
     * 广宣单个预算审核
     * @param number $id
     * @param number $audit
     * @param array $data
     * @return object
     */
    function budgetAudit($id, $audit = 1, $user = array()){
        $findData = $this->getData($id);
        $auditData = array(
            'budgetAudit' =>  $audit==1 ? $findData['budgetApply'] : 0,
            'budgetAuditRs' => $audit,
            'budgetAuditRole' => $user['userRole']
        );
        return $this->db->update($this->table, $auditData,array('id'=>$id));
    }
    /**
     * 结算审核状态统计
     * @param int $procId
     * @return multitype:
     */
    function getSettleStatusCountProcId($procId, $isBubao = 0) {
        $findList = $this->getList(array('procId'=>$procId,'isBubao'=>$isBubao,'isSettle'=>1,'budgetAuditRs' => array('in',array(1,4))), null, null, 'settleAuditRs');
        $count = array();
        foreach ($findList as $v){
            $count[$v['settleAuditRs']] ++;
        }
        return $count;
    }
    /**
     * 结算审核状态统计
     * @param int $procId
     * @return multitype:
     */
    function getSettleStatusCountProjectId($projectId) {
        $findList = $this->getList(array('projectId'=>$projectId,'isSettle'=>1,'budgetAuditRs' => array('in',array(1,4))), null, null, 'settleAuditRs');
        $count = array();
        foreach ($findList as $v){
            $count[$v['settleAuditRs']] ++;
        }
        return $count;
    }
    /**
     * 广宣通过主线ID一键结算审核
     * @param int $procId
     * @param number $audit
     * @param array $user
     */
    function settleAuditByProcId($procId, $audit = 1,$user = array()){
        $findList = $this->getList(array('procId'=>$procId, 'isSettle'=>1));
        foreach ($findList as $v){
            $auditData = array(
                'settleAudit' => $audit==1 ? $v['settleApply'] : 0,
                'settleAuditRs' => $audit,
                'settleAuditRole' => $user['userRole']
            );
            $this->db->update($this->table, $auditData, array('id'=>$v['id']));
        }
    }
    /**
     * 广宣通过项目ID一键结算审核
     * @param int $procId
     * @param number $audit
     * @param array $user
     */
    function settleAuditByProjectId($projectId, $audit = 1,$user = array()){
        $findList = $this->getList(array('projectId'=>$projectId, 'isSettle'=>1));
        foreach ($findList as $v){
            $auditData = array(
                'settleAudit' => $audit==1 ? $v['settleApply'] : 0,
                'settleAuditRs' => $audit,
                'settleAuditRole' => $user['userRole']
            );
            $this->db->update($this->table, $auditData, array('id'=>$v['id']));
        }
    }
    /**
     * 结算单个审核
     * @param number $id
     * @param number $audit
     * @param array $user
     * @return object
     */
    function settleAudit($id, $audit = 1, $user = array()){
        $findData = $this->getData($id);
        $auditData = array(
            'settleAudit' =>  $audit==1 ? $findData['settleApply'] : 0,
            'settleAuditRs' => $audit,
            'settleAuditRole' => $user['userRole']
        );
        return $this->db->update($this->table, $auditData,array('id'=>$id));
    }
}